package com.gxa.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.R;
import com.gxa.finance.mapper.PricingMapper;
import com.gxa.finance.pojo.Pricing;
import com.gxa.finance.service.PricingService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PricingServiceImpl extends ServiceImpl<PricingMapper, Pricing> implements PricingService {
    @Autowired
    private PricingMapper pricingMapper;
    @Override
    public R addPricing(Pricing pricing) {
        R r = new R();
        try {
            pricingMapper.insert(pricing);
//            QueryWrapper<Pricing> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq(pricing.getPType()!=null&&!pricing.getPType().isEmpty(),"p_type",pricing.getPType());
//            List<Pricing> pricings = pricingMapper.selectList(queryWrapper);
            R pricing1 = this.findPricing(pricing.getpType());
            r.setCode(200);
            r.setMsg("添加成功");
            r.setData(pricing1.getData());
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("添加失败");

        }
        return r;
    }

    @Override
    public R findPricing(String pType) {
        R r = new R();
        try {
            QueryWrapper<Pricing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(pType!=null&&!pType.isEmpty(),"p_type",pType);
            List<Pricing> pricings = pricingMapper.selectList(queryWrapper);

            r.setData(pricings);
            r.setCode(200);
            r.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("查询失败");
        }
        return r;
    }


    @Override
    public R addPricingList(List<Pricing> pricings) {
        R r = new R();
        try {
            for (Pricing pricing : pricings) {
                pricingMapper.insert(pricing);
            }
            r.setCode(200);
            r.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    @Override
    public R delByIdPricing(Integer id) {
        R r = new R();
        try {
            pricingMapper.deleteById(id);
            r.setCode(200);
            r.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }

    @Override
    public R updateByIdPricing(Pricing pricing) {
        R r = new R();
        try {
            pricingMapper.updateById(pricing);
            r.setCode(200);
            r.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("修改失败");
        }
        return r;
    }
}
